package com.thirtydays.frame.ui.text

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.FrameLayout

import com.thirtydays.frame.R
import com.thirtydays.frame.databinding.LayoutImageTextViewBinding
import com.thirtydays.frame.util.ext.gone
import com.thirtydays.frame.util.ext.visible


/**
 * @author 小垚
 * @创建时间： 2021/5/22
 * @描述：
 **/
class ImageTextView : FrameLayout {
    constructor(context: Context) : super(context) {
        initView(context, null, -1, -1)
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        initView(context, attrs, -1, -1)
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        initView(context, attrs, defStyleAttr, -1)
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes) {
        initView(context, attrs, defStyleAttr, defStyleRes)
    }

    lateinit var viewBinding: LayoutImageTextViewBinding
    private fun initView(context: Context, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int) {
        viewBinding = LayoutImageTextViewBinding.inflate(LayoutInflater.from(context), this, true)
        val attrs = context.obtainStyledAttributes(attrs, R.styleable.ImageTextView)

        var iconDrawable = attrs.getResourceId(R.styleable.ImageTextView_icon_drawable, -1)
        var textValue = attrs.getString(R.styleable.ImageTextView_text_value)
        var textTip = attrs.getString(R.styleable.ImageTextView_text_tip)
        var isShow = attrs.getBoolean(R.styleable.ImageTextView_isShowArrow, true)
        var isShowLine = attrs.getBoolean(R.styleable.ImageTextView_iv_tv_isShowLine, true)


        if (iconDrawable != -1) {
            viewBinding.iconIv.setImageResource(iconDrawable)
        }
        viewBinding.valueTv.text = textValue
        viewBinding.tipTv.text = textTip

        if (isShow) {
            viewBinding.arrowIv.visible()
        } else {
            viewBinding.arrowIv.gone()
        }

        if (isShowLine) {
            viewBinding.vLine.visible()
        } else {
            viewBinding.vLine.gone()
        }
    }
}